package org.example.deep.utils.str;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 脱敏工具类
 */
public class RegexUtils extends StringUtils {

    public static String encryAddress(String address) {
        if (address != null) {
            if (address.indexOf("上海市") != -1) {
                //结构化
                return address.replaceAll("(.{1,}市(.{1,}区)?)(.*)(.{5})", "$1******$4");
            } else {
                //非结构化
                int length = address.length();
                if (length < 3) {
                    return address;
                } else if (length < 6) {
                    return address.replaceAll("(.{1})(.*)(.{2})", "$1" + getStars(length - 3) + "$3");
                } else if (length < 10) {
                    return address.replaceAll("(.*)(.{5})", getStars(length - 5) + "$2");
                } else {
                    return address.replaceAll("(.*)(.{4})(.{5})", "$1****$3");
                }

            }
        }
        return address;
    }

    public static String enceyBankNo(String bankNo) {
        if (bankNo.length() >= 13) {
            bankNo = bankNo.substring(0, 4) + "********" + bankNo.substring(bankNo.length() - 4);
        }
        return bankNo;
    }

    /**
     * 社会统一信用代码
     *
     * @param string
     * @return
     */
    public static String enceySHTYXYDM(String string) {
        char[] chars = string.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (i < 4 || i >= chars.length - 4) {
                sb.append(chars[i]);
            } else {
                sb.append("*");
            }
        }
        return sb.toString();
    }

    public static String encryName(String name) {
        if (name != null) {
            String[] splits = name.split("\\.");
            String result = Arrays.asList(splits).stream().map(str -> {
                int length = str.length();
                if (length < 2) {
                    //一个字 不脱敏
                    return str;
                } else if (length < 4) {
                    // 两到三个字 脱敏第一个字
                    return str.replaceAll("(.{1})(.*)", "*$2");
                } else if (length < 7) {
                    // 四到六个字 脱敏前两个字
                    return str.replaceAll("(.{2})(.*)", "**$2");
                } else {
                    // 6个字以上（不含） 脱敏第三到六个字
                    return str.replaceAll("(.{2})(.{4})(.*)", "$1****$3");
                }
            }).collect(Collectors.joining("."));
            return result;
        }
        return "";
    }

    public static String encryNameByConsType(String name, String consType) {
        if ("03".equals(consType)) {
            return encryName(name);
        } else {
            if (name != null) {
                int length = name.length();
                if (length < 3) {
                    //2字及以下 不脱敏
                    return name;
                } else if (length < 5) {
                    //4字及以下 首位保留各保留1个
                    return name.replaceAll("(.{1})(.*)(.{1})", "$1*$3");
                } else if (length < 7) {
                    // 5-6个字 首位保留各保留2个
                    return name.substring(0, 2) + (length == 5 ? "*" : "**") + name.substring(length - 2);
                } else if (length % 2 == 1) {
                    // 7字及以上 奇数隐藏中间三个字
                    return name.substring(0, (length - 3) / 2) + "***" + name.substring(3 + (length - 3) / 2);
                } else {
                    // 8字及以上 奇数隐藏中间四个字
                    return name.substring(0, (length - 4) / 2) + "****" + name.substring(4 + (length - 4) / 2);
                }
            }
        }
        return "";
    }

    public static String encryHuhao(String huhao) {
        if (huhao != null) {
            return huhao.replaceAll("([0-9]{3})([0-9]{1,4})([0-9]{3})", "$1****$3");
        }
        return huhao == null ? "" : huhao;
    }


    public static String valueOf(Object obj) {
        return (obj == null) ? "" : obj.toString();
    }


    public static String encryPhone(String phone) {
        if (phone != null) {
            return phone.replaceAll("([0-9]{3})([0-9]{1,4})([0-9]{4})", "$1***$3");
        }
        return phone == null ? "" : phone;
    }

    /**
     * @param idCard
     * @return 00传入为空对象或空字符串 01 长度不符合规则 02 未通过正则表达式校验
     */
    public static String encryIdCard(String idCard) {
        if (RegexUtils.isBlank(idCard)) {
            return "00";
        }
        if (idCard.length() != 18) {
            return "01";
        }
        if (!idCard.matches("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$")) {
            return "02";
        }
        return idCard.substring(0, 7) + "********" + idCard.substring(14);
    }

    public static String getStars(int num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            sb.append("*");
        }
        return num < 1 ? "" : sb.toString();
    }

}
